package za.org.jcicada.genss.sensor.impl;

import java.net.URISyntaxException;

import org.apache.commons.logging.LogFactory;

import za.org.jcicada.genss.sensor.ISensor;
import za.org.jcicada.genss.sensor.ISensorValue;
import za.org.jcicada.genss.sensor.SensorURN;

public class StrategyDifferentialFloat extends StrategyDifferential {
	private static final long serialVersionUID = 1L;
	
    /**
     * Construct event strategy and initialise status and value so that on update
     * it will immediately notify.
     * @param sensorURN the sensor URI
     * @param threshold difference threshold to be exceeded before notify
     */
    public StrategyDifferentialFloat(SensorURN sensorURN, float threshold) {
        super(sensorURN, threshold);
    }
    
    /**
     * Construct strategy from string array
     * @param args created by toStringArray
     * @throws URISyntaxException
     */
    public StrategyDifferentialFloat(String[] args) throws URISyntaxException {
        this(new SensorURN(args[0]), Float.parseFloat(args[1]));
    }
    
    /**
     * Override the attachToSensor method to check that the sensor is a float
     * sensor.
     * @param sensor must be a float sensor
     */
    @Override
    public boolean attachToSensor(ISensor sensor) {
        if (!(sensor.getType() instanceof TypeFloat)) {
            LogFactory.getLog(StrategyDifferentialFloat.class).error("Can only use StrategyDifferentialFloat with TypeFloat sensors");
            return false;
        }
        else {
            return super.attachToSensor(sensor);
        }
    }
    
    @Override
    protected double getValueAsDouble(ISensorValue value) {
        return ((Float)value.getValue()).doubleValue();
    }

    public String[] toStringArray() {
        return new String[]{getSensorURN().toString(), Float.toString((float)threshold)};
    }

}
